using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WP.DB;
using WP.Commons;
namespace WP.Modules
{
    public partial class frmUIOpenAndNewDailg : BaseQryRpt
    {
        private bool mblnIsCancel = true ;
        private bool mblnIsSelectDesignStyle = true;
        public frmUIOpenAndNewDailg()
        {
            InitializeComponent();
        }

        public string SelectDesignStyle(ref string strMenuId)
        {
            this.Text = "Select style";
            mblnIsSelectDesignStyle = true;
            CreateMenu();
            if (strMenuId != null)
            {
                TreeNode[] nodx = tvwMenu.Nodes.Find(strMenuId, true);
                if (nodx.Length > 0) tvwMenu.SelectedNode = nodx[0];
            }
            FillStyle(strMenuId);
            InitControl();
            this.ShowDialog();
            this.Close();
            if (!mblnIsCancel)
            {
                strMenuId = tvwMenu.SelectedNode.Name;
                return lvwStyle.SelectedItems[0].Text;
            }
            else
            {
                strMenuId = "";
                return "";
            }
 
        }

        public string SelectMenuId(string strMenuId)
        {

            this.Text = "Select Program";
            mblnIsSelectDesignStyle = false;
            this.lvwStyle.Visible = false;
            
            CreateMenu();
            if (strMenuId != null)
            {
                TreeNode[] nodx = tvwMenu.Nodes.Find(strMenuId, true);
                if (nodx.Length > 0)
                {
                    tvwMenu.SelectedNode = nodx[0];
                    btnOk.Enabled = true; 
                }

            }
            InitControl();

            this.ShowDialog();
            this.Close();
            if (!mblnIsCancel)
            {
                return tvwMenu.SelectedNode.Name;  
            }
            else
                return "";

        }

        private void CreateMenu()
        {
            tvwMenu.Nodes.Clear();
            DBAccess dbcon = new DBAccess(false, false);
            DataView dvMenu = dbcon.GetDataTable("select * from mMenu where (FormStyle='D' and net_progname>'') or net_Progname='' order by menuid").DefaultView ;
            TreeNode nodx=null ;
            TreeNode [] nodParent;
            foreach(DataRowView drw in dvMenu)
            {
                nodx = new TreeNode(drw["MenuName"].ToString().Trim());
                nodx.Tag = drw["MenuID"].ToString().Trim() + "|" + drw["net_progname"].ToString().Trim() + "|" + drw["Net_Parameter"].ToString().Trim();
                nodx.Name = drw["MenuID"].ToString().Trim();
                if (tvwMenu.Nodes.Count >0)
                {
                    string strCurrentMenuId = drw["MenuID"].ToString().Trim();
                    string strParentMenuId = GetParentMenuId(strCurrentMenuId);
                    if (strParentMenuId != "")
                    {
                        nodParent = tvwMenu.Nodes.Find( strParentMenuId,true);
                        if(nodParent.Length >0) nodParent[0].Nodes.Add(nodx);          
                    }
                    else
                        tvwMenu.Nodes.Add(nodx);   
                }
                else
                    tvwMenu.Nodes.Add(nodx);   
            }
            dbcon.CloseCon();  
        }

        private void FillStyle(string strMenuId)
        {
            DBAccess dbcon = new DBAccess(false, false);
            MenuInfo minfo = DataFunctions.GetMenuInfo(dbcon, strMenuId);
            DataView dvStyle = dbcon.GetDataTable("select distinct UiStyle  from mFormDict where Progname<>'' and progname='" + minfo.strProgname +"' and parameter=" + minfo.iParameter.ToString()).DefaultView;
            lvwStyle.Items.Clear();
            lvwStyle.View = View.LargeIcon; 
            foreach(DataRowView drw in dvStyle)
            {
                ListViewItem ltem = new ListViewItem(drw["UiStyle"].ToString().Trim());
                ltem.ImageIndex  = 0;
                lvwStyle.Items.Add(ltem);     
            }
            dbcon.CloseCon(); 
        }


        private string GetParentMenuId(string strMenuId)
        {
            if (strMenuId.Substring(3, 3) != "000" && strMenuId.Substring(6, 6) == "000000") return strMenuId.Substring(0, 3) + "000000000";
            if (strMenuId.Substring(3, 3) != "000" && strMenuId.Substring(6, 3) != "000" && strMenuId.Substring(9, 3) == "000") return strMenuId.Substring(0, 6) + "000000";
            if (strMenuId.Substring(3, 3) != "000" && strMenuId.Substring(6, 3) != "000" && strMenuId.Substring(9, 3) != "000") return strMenuId.Substring(0, 9) + "000";
            return "";
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (mblnIsSelectDesignStyle)
            {
                if (lvwStyle.SelectedItems.Count > 0)
                {
                    mblnIsCancel = false;
                    this.Hide();
                }
                else
                {
                    return;
                }
            }
            else
            {
                string[] strTag = tvwMenu.SelectedNode.Tag.ToString().Split(new char[]{'|'});
                if (strTag.Length > 1)
                {
                    mblnIsCancel = false;
                    this.Hide();
                }
                else
                {
                    return;
                }
            }
        }

        private void tvwMenu_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            
            if (mblnIsSelectDesignStyle)
            {
                FillStyle(e.Node.Name);
                this.btnOk.Enabled = false; 
            }
            else
            {
                string[] strTag = e.Node.Tag.ToString().Split(new char[] { '|' });
                if (strTag.Length > 1 && strTag[1].Trim()!="" )
                {
                    this.btnOk.Enabled = true ; 
                }
                else
                {
                    this.btnOk.Enabled = false; 
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            mblnIsCancel = true;
            this.Hide(); 
        }

        private void lvwStyle_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.btnOk.Enabled = true; 
        }

        private void InitControl()
        {
            if (mblnIsSelectDesignStyle)
            {
                lblTitle.Text = "Select a user defined user style ";
                lvwStyle.Visible = true;
                tvwMenu.Width = lvwStyle.Left - tvwMenu.Left - 20;
                tvwMenu.Visible = true;
            }
            else
            {
                lblTitle.Text = "Select a program to design user style "; 
                tvwMenu.Width = lvwStyle.Left + lvwStyle.Width - tvwMenu.Left;
                lvwStyle.Visible = false;
                tvwMenu.Visible = true;
            }
        }

        private void lvwStyle_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (lvwStyle.SelectedItems.Count > 0)
            {
                btnOk_Click(this, new EventArgs()); 
            }
        }
    }
}